package com.powernobug.util;

import com.powernobug.bean.model.MarketAdmin;
import com.powernobug.bean.model.MarketStorage;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-23
 * @Description:
 */
public class VerifyUtil {
    private VerifyUtil() {}

    public static boolean storageInfoVerify(MarketStorage origin, MarketStorage verify, boolean deleteMode) throws NoSuchFieldException, IllegalAccessException {
        Class verifyClass = verify.getClass();
        Class originClass = origin.getClass();
        for (Field field : verifyClass.getDeclaredFields()) {
            String name = field.getName();
            // 只改name，所以遇到 name 字段跳过，add_time和update_time也可以跳过
            if (name.equals("name") || name.equals("addTime") || name.equals("updateTime"))
                continue;
            // 如果 deleteMode 为 true，则再次忽略“delete”字段
            if (deleteMode && name.equals("deleted"))
                continue;
            Field originClassField = originClass.getDeclaredField(name);
            // 设置字段为可访问状态
            field.setAccessible(true);
            originClassField.setAccessible(true);
            // 获取字段的值
            Object verifyValue = field.get(verify);
            Object originValue = originClassField.get(origin);
            // 比较两个字段的值
            if (!Objects.equals(verifyValue, originValue)) {
                return false;
            }
        }
        return true;
    }
}
